<?php
// +-------------------------------------------------------
// | Name： 星空框架  订单模型
// +-------------------------------------------------------
// | Version: V5.0
// +-------------------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +-------------------------------------------------------
// | Time: 2021-04-25 13：19
// +-------------------------------------------------------
declare(strict_types=1);

namespace app\common\model;

use think\model\concern\SoftDelete;

/**
 * @xk 订单模型
 */
class SubOrder extends BaseModel
{
  use SoftDelete;
  protected $name = 'subOrder';
  protected $pk = 'sub_order_id';
  protected $deleteTime = 'delete_time';
  /**
   * @xk 写入前操作
   *
   * @method onBeforeWrite
   * @param  [type]                  $data                     [description]
   * @return [type]                                            [description]
   * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
   * @Time   2021-04-22T17:31:28+080
   */
  public static function onBeforeWrite($data)
  {
      unset($data['act']);
      unset($data['controllers']);
      unset($data['actions']);
      if (!$data->create_time) {
          $data->update_time = $data->create_time = date('Y-m-d H:i:s');
      }
      $data->update_time = date('Y-m-d H:i:s'); // 操作时间 实际发布时间
  }
  /**
   * @xk 订单附属产品
   *
   * @method orderGoods
   * @return [type]     [description]
   * @Time   2021-10-22
   * @City   青岛
   * @Author xkyinzhe   <zhonghuaxinxing@sina.cn>
   */
  public  function orderGoods(){
    return $this->hasMany('goodsOrder', 'sub_order_sn', 'sub_order_sn');
  }
  /**
   * @xk 生成订单核销码
   *
   * @method createSelfCode
   * @param  [type]         $user_sn                   [description]
   * @return [type]         [description]
   * @Time   2021-10-22
   * @City   青岛
   * @Author xkyinzhe       <zhonghuaxinxing@sina.cn>
   */
  public static function createSelfCode($sub_order_sn){
    if(!$sub_order_sn){
      return  '';
    }
    require_once(__DIR__ . '/../../../extend/phpQrcode/phpqrcode.php');
    $path = 'upload/order/selfQrcode/'; // 用户生成的会员码 每个用户一个
    $share_url = 'https://'.$_SERVER['HTTP_HOST'].'/#/pages/self_check/self_check?sub_order_sn='.base64_encode($sub_order_sn);
    $qrcode = new \QRcode();
    $value = $share_url;                    //二维码内容
    $errorCorrectionLevel = 'H';    //容错级别
    $matrixPointSize = 3;           //生成图片大小
    ob_start();
    if(!file_exists($path)){
      mkdir($path, 0777, true);
    }
    $qrcodename = $path.'/'.$sub_order_sn.'.png';
    $qrcode::png($value,$qrcodename , $errorCorrectionLevel, $matrixPointSize, 2);
    imagecreatefromstring(file_get_contents($qrcodename));
    return $qrcodename;
  }
}
